package com.youdoneed.community.forum.service;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import com.youdoneed.base.application.AppException;
import com.youdoneed.community.common.utils.StringUUIdUtil;
import com.youdoneed.community.forum.dao.ForumsystemrecommenduserMapper;
import com.youdoneed.community.forum.entity.Forumsystemrecommenduser;

/**
 * 系统推荐的用户
 *
 * @author lid
 * @date 2017.6.15
 */
@Service
public class ForumsystemrecommenduserService {

    private final Logger logger = LoggerFactory.getLogger(ForumsystemrecommenduserService.class);

    @Autowired
    ForumsystemrecommenduserMapper mapper;

    /**
     * 保存系统推荐的用户
     *
     * @param rel
     * @return int
     * @author lid
     * @date 2017.6.15
     */
    @Transactional
    public int saveForumsystemrecommenduser(Forumsystemrecommenduser user) {
        logger.info("saveForumsystemrecommenduser");
        if (null == user) {
            throw new AppException("rel参数为空！");
        }

        user.setForumSystemRecommendUserId(StringUUIdUtil.getUUID());
        user.setCreateDate(new Date());

        try {
            return mapper.insert(user);
        } catch (Exception e) {
            logger.info("保存系统推荐的用户失败！" + e);
            return 0;
        }
    }

    /**
     * 删除系统推荐的用户
     *
     * @param id
     * @return int
     * @author lid
     * @date 2017.6.15
     */
    @Transactional
    public int deleteForumsystemrecommenduser(String id) {
        logger.info("deleteForumsystemrecommenduser");
        if (!StringUtils.hasText(id)) {
            throw new AppException("id参数为空！");
        }

        try {
            return mapper.deleteByPrimaryKey(id);
        } catch (Exception e) {
            logger.info("删除关注标签记录失败！" + e);
            return 0;
        }
    }

    /**
     * 获取所有的推荐用户列表
     *
     * @return List<Forumsystemrecommenduser>
     * @author lid
     * @date 2017.6.16
     */
    @Transactional
    public List<Forumsystemrecommenduser> getAllRecommendUsers(int offset, int limit) {
        logger.info("getAllRecommendUsers");
        PageHelper.startPage(limit, offset);
        try {
            return mapper.getAllRecommendUsers();
        } catch (Exception e) {
            logger.info("获取所有的推荐用户列表失败！" + e);
            return null;
        }
    }

    /**
     * 统计所有系统推荐用户数量
     *
     * @return
     * @author lixs
     * @Date 2017.6.22
     */
    @Transactional
    public int countTotalCommendUser() {
        return mapper.countTotalCommendUser();
    }

    /**
     * 根据条件分页查询系统推荐用户
     *
     * @param nickname
     * @param createdateS
     * @param createdateE
     * @param rows
     * @param currentPage
     * @author lixs
     * @Date 2017.6.22
     */
    @Transactional
    public List<Forumsystemrecommenduser> queryRecommendUsersByPage(String nickname, String createdateS, String createdateE, int rows, int currentPage) {
        logger.info("queryRecommendUsersByPage");
        PageHelper.startPage(currentPage, rows);
        try {
            return mapper.queryRecommendUsers(nickname, createdateS, createdateE);
        } catch (Exception e) {
            logger.info("获取条件查询的推荐用户列表失败！" + e);
            return null;
        }

    }

    /**
     * 根据条件查询系统推荐用户（用于统计用户数量）
     *
     * @param nickname
     * @param createdateS
     * @param createdateE
     * @author lixs
     * @Date 2017.6.22
     */
    @Transactional
    public List<Forumsystemrecommenduser> queryRecommendUsers(String nickname, String createdateS, String createdateE) {
        logger.info("queryRecommendUsers");
        try {
            return mapper.queryRecommendUsers(nickname, createdateS, createdateE);
        } catch (Exception e) {
            logger.info("获取条件查询的推荐用户列表失败！" + e);
            return null;
        }

    }

    /**
     * 修改系统推荐用户信息
     *
     * @param record
     * @author lixs
     * @Date 2017.6.23
     */
    @Transactional
    public int updateByPrimaryKeySelective(Forumsystemrecommenduser record) {
        logger.info("updateByPrimaryKeySelective");
        try {
            return mapper.updateByPrimaryKeySelective(record);
        } catch (Exception e) {
            logger.info("修改推荐用户信息失败！" + e);
            return 0;
        }
    }


}
